package com.yanq.liancar.adapter;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.nex3z.flowlayout.FlowLayout;
import com.yanq.liancar.R;
import com.yanq.liancar.bean.ShopListBean;

import java.math.BigDecimal;
import java.util.List;

public class ShopListAdapter extends BaseQuickAdapter<ShopListBean.DataBean, BaseViewHolder> {
    public ShopListAdapter(int layoutResId, @Nullable List<ShopListBean.DataBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, ShopListBean.DataBean item) {
        helper.setText(R.id.shop_title, item.getShopName())
                .setText(R.id.shop_address, item.getShopAddress())
                //.setText(R.id.tv_service,item.getServer())
                .setText(R.id.tv_shoppinglun, item.getRamarkNum() + "")
                .setText(R.id.text, item.getAttenNum() + "");

        helper.setText(R.id.tv_shopaddress, (double) new BigDecimal(item.getLang()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "Km");
        FlowLayout flowLayout = helper.getView(R.id.flow);
        ImageView imageView = helper.getView(R.id.img_logo);
        if (!item.getShopPages().isEmpty()){
            RequestOptions options=new RequestOptions();
            options.placeholder(R.mipmap.item_fuwu);
            Glide.with(mContext).load(item.getShopPages().get(0).getWebPath()).apply(options).into(imageView);
        }

        if (!item.getTypeList().isEmpty()) {
            flowLayout.removeAllViews();
            for (int i = 0; i < item.getTypeList().size(); i++) {
                TextView textView = buildLabel(item.getTypeList().get(i));
                flowLayout.addView(textView);
            }
        }
    }

    private TextView buildLabel(final String text) {
        TextView textView = new TextView(mContext);
        textView.setText(text);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
        textView.setTextColor(mContext.getResources().getColor(R.color.main_color));
        textView.setPadding((int) dpToPx(8), (int) dpToPx(8), (int) dpToPx(8), (int) dpToPx(8));
        textView.setBackgroundResource(R.drawable.textview_border_blue2);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //et.setText(text);
            }
        });
        return textView;
    }

    private float dpToPx(float dp) {
        return TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, dp, mContext.getResources().getDisplayMetrics());
    }

}
